package com.jic.market.impl.base;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.utils.IDUtils;
import com.jic.market.entity.base.BaseActivity;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.mapper.base.BaseActivityMapper;
import com.jic.market.request.CouponConfigQueryRequest;
import com.jic.market.request.MemberGradeInfoRequest;
import com.jic.market.request.manager.base.BaseActivityRequest;
import com.jic.market.response.CouponConfigQueryResponse;
import com.jic.market.service.PromotionMemberGradeService;
import com.jic.market.service.base.BaseActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: gaolanyu
 * @date: 2020-03-10
 * @remark: 只保存营销活动的公共字段 其余字段各活动具体impl自己维护
 */
@Service
@Slf4j
public class BaseActivityServiceImpl implements BaseActivityService {
    @Autowired
    private BaseActivityMapper baseActivityMapper;
    @Autowired
    private PromotionMemberGradeService promotionMemberGradeService;
    @Override
    public PageResult<BaseActivity> queryPage(BaseActivity baseActivity, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<BaseActivity> list = baseActivityMapper.queryPage(startIndex, pageQuery.getLimit(), baseActivity);
        long count = baseActivityMapper.queryPageCount(baseActivity);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
    @Override
    public RestResult<String> save(BaseActivityRequest baseActivityRequest) {
        BaseActivity baseActivity=new BaseActivity();
        BeanUtils.copyProperties(baseActivityRequest,baseActivity);
        baseActivity.setPromotionId(IDUtils.GenerateBillNo(baseActivityRequest.getFrontPromType()));
        baseActivity.setStatus("9");
        baseActivity.setDeleteFlag(0);
        baseActivity.setCreatorName(baseActivityRequest.getOperateName());
        baseActivity.setCreator(baseActivityRequest.getOperator());
        baseActivity.setDeptId(baseActivityRequest.getOperatorDeptId());
        baseActivity.setChannelCodes(baseActivityRequest.getChannelCodes());

        baseActivity.setParentAllocationScale(baseActivityRequest.getParentAllocationScale() == null ? 0 : baseActivityRequest.getParentAllocationScale());
        baseActivity.setChildAllocationScale(baseActivityRequest.getChildAllocationScale() == null ? 0 : baseActivityRequest.getChildAllocationScale());
        baseActivity.setMerchantAllocationScale(baseActivityRequest.getMerchantAllocationScale() == null ? 0 : baseActivityRequest.getMerchantAllocationScale());
        baseActivity.setStoreAllocationScale(baseActivityRequest.getStoreAllocationScale() == null ? 0 : baseActivityRequest.getStoreAllocationScale());

        baseActivityMapper.insert(baseActivity);

        MemberGradeInfoRequest memberGradeInfoRequest = new MemberGradeInfoRequest();
        memberGradeInfoRequest.setPromotionId(baseActivity.getPromotionId());
        memberGradeInfoRequest.setPromotionCode(baseActivity.getPromotionId());
        memberGradeInfoRequest.setPromType(Integer.parseInt(baseActivity.getPromType()));
        memberGradeInfoRequest.setFrontPromType(Integer.parseInt(baseActivity.getFrontPromType()));
        memberGradeInfoRequest.setPromotionMemberGradeList(baseActivityRequest.getPromotionMemberGradeList());
        promotionMemberGradeService.addMemberGrade(memberGradeInfoRequest);

        return RestResult.success(baseActivity.getPromotionId());
    }

    @Override
    public Long update(BaseActivity baseActivity) {
        return baseActivityMapper.update(baseActivity);
    }

    @Override
    public BaseActivity queryActivityByPromotionId(String promotionId) {
        return baseActivityMapper.queryByPromotionId(promotionId);
    }

    @Override
    public BaseActivity queryActivityById(Long id) {
        return baseActivityMapper.queryByPrimaryKey(id);
    }

    @Override
    public RestResult<String> copy(String promotionId,boolean templateFlag) {
        // TODO 创建人更新人字段
        BaseActivity activity=queryActivityByPromotionId(promotionId);
        String oldPromotionId = activity.getPromotionId();
        if(activity==null){
            return RestResult.error("9999","promotionId查不到数据");
        }
        Date now=new Date();
        activity.setCreateTime(now);
        activity.setUpdateTime(now);
        activity.setDeleteFlag(0);
        activity.setPromotionId(IDUtils.GenerateBillNo(activity.getFrontPromType()));
        if(!templateFlag) {
            activity.setStatus("9");
        }else{
            activity.setStatus("10");
        }
        activity.setPromName(activity.getPromName() + "-复制");
        baseActivityMapper.insert(activity);
        promotionMemberGradeService.copyMemberGrade(oldPromotionId, activity.getPromotionId(), activity.getPromotionId());
        return RestResult.success(activity.getPromotionId());
    }

    @Override
    public Long updateStatusByPromotionId(String promotionId, String status) {
        return baseActivityMapper.updateStatusByPromotionId(promotionId,status);
    }

    @Override
    public Long deleteByPromotionId(String promotionId) {
        return baseActivityMapper.delete(promotionId);
    }

    @Override
    public Long check(String promotionId, String remark, String status) {
        return baseActivityMapper.check(promotionId,remark,status);
    }

    @Override
    public PageResult<BaseActivity> querySkillPage(BaseActivity baseActivity,Page pageQuery, String merchantId, String storeId) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<BaseActivity> list = baseActivityMapper.querySkillPage(
                startIndex,
                pageQuery.getLimit(),
                baseActivity,
                merchantId,
                storeId);
        Long count = baseActivityMapper.querySkillPageCount(
                baseActivity,
                merchantId,
                storeId);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public PageResult<BaseActivity> queryCouponPage(BaseActivity baseActivity, Page pageQuery, String merchantId, String storeId, String grantType, Integer couponType) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<BaseActivity> list = baseActivityMapper.queryCouponPage(
                startIndex,
                pageQuery.getLimit(),
                baseActivity,
                merchantId,
                storeId,
                grantType,
                couponType);
        long count = baseActivityMapper.queryCouponPageCount(
                baseActivity,
                merchantId,
                storeId,
                grantType,
                couponType);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public BaseActivity queryByPromotionIdAndPromType(String promotionId, String promType) {
        return baseActivityMapper.queryByPromotionIdAndPromType(promotionId,promType);
    }

    @Override
    public PageResult<CouponConfigQueryResponse> getCouponList(CouponConfigQueryRequest couponConfigQueryRequest) {
        couponConfigQueryRequest.setStart((couponConfigQueryRequest.getStart() - 1) * couponConfigQueryRequest.getLimit());
        List<CouponConfigQueryResponse> rest = baseActivityMapper.getCouponList(couponConfigQueryRequest);
        Integer count = baseActivityMapper.getCouponListCount(couponConfigQueryRequest);
        return new PageResult<CouponConfigQueryResponse>(){{
            setRows(rest);
            setTotal(count);
        }};
    }

    @Override
    public CouponConfigQueryResponse getCouponByPromotionId(CouponConfigQueryRequest couponConfigQueryRequest) {
        return baseActivityMapper.getCouponByPromotionId(couponConfigQueryRequest);
    }

}
